//
//  ECPrivateLetterViewController.m
//  Edu_iOS2018
//
//  Created by apple on 2018/1/12.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECPrivateLetterViewController.h"
#import "ECPrivateLetterTableViewCell.h"
#import "ECPrivateLetterDetailVc.h"
#import "ECPrivateLetterModel.h"
#import "CellModelFrame.h"

@interface ECPrivateLetterViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (nonatomic, strong) UITableView *messageTBview;
@property (nonatomic, strong) NSMutableArray *listTBArray;
///总个数
@property (nonatomic, assign) int allPageNum;

@end

@implementation ECPrivateLetterViewController



-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.messageTBview];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];

    [self getDataWithURL];
}

/*  加载数据  */
-(void)getDataWithURL{
    [HWHttpTool requestWithMethod:@"GET"
                              url:URL_GET_Mine_Messages
                           params:nil
                          headers:@{@"X-AUTH-TOKEN":[[UserInfoManager shareUser] token]}
                          success:^(id json) {
                              [self.messageTBview.mj_footer endRefreshing];//结束刷新
                              self.allPageNum = [json[@"data"][@"total"] intValue];//总个数
                              
                              NSArray *array = json[@"data"][@"resources"];
                              [self.listTBArray addObjectsFromArray: [ECPrivateLetterModel mj_objectArrayWithKeyValuesArray:array]];
                              //判断数组个数,隐藏footer
                              if (_listTBArray.count >= self.allPageNum) {
                                  [self.messageTBview.mj_footer endRefreshingWithNoMoreData];
                              }
                              [self.messageTBview reloadData];
                          }
                          failure:^(NSError *error) {
                              [self.messageTBview.mj_footer endRefreshing];
                              NSString *str = [ECData_Json requestErrorTipWith:error];
                              if (!([str isEqualToString:@"101"] || [str isEqualToString:@"401"])) {
                                  [MBProgressHUD showTipMessageInWindow:kAlertTipOfInternetFailure];
                              }
                          }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listTBArray.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indetify = @"ECPrivateLetterTableViewCell";
    ECPrivateLetterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetify];
    if (!cell) {
        cell = [[ECPrivateLetterTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetify];
    }
    [cell setContentMode:self.listTBArray[indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ECPrivateLetterDetailVc *vc =[[ECPrivateLetterDetailVc alloc]init];
    ECPrivateLetterModel *model = self.listTBArray[indexPath.row];
    model.unreadNum = @"0";//手动设置未读消息为0，在进入列表页面刷新数据，不在请求数据
    vc.ID = model.ID;//传递ID
    vc.nickname = model.nickname;//传递nickname;
    vc.smallAvatar = model.smallAvatar;//传递smallAvatar
    [self.navigationController pushViewController:vc animated:YES];
    
    vc.block_back = ^(id obj) {
        CellModelFrame *cellModelFrame = (CellModelFrame *)obj;
        model.latestMessageContent = cellModelFrame.cellModel.content;
        NSString *time = [cellModelFrame.cellModel.createdTime substringWithRange:NSMakeRange(0,16)];//格式时间
        model.latestMessageTime = time;
        
        [self.messageTBview reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];//刷新点击的行
    };
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kProportion_Width(235);
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"Mine_emptyImage_SystemNoti"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
    NSString *text = @"暂无任何私信";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f],
                                 NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#b3b3b3"]};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

-(UITableView *)messageTBview{
    if (!_messageTBview) {
        _messageTBview = [[UITableView alloc]init];
        _messageTBview.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-kProportion_Height(138 + 12) - kNavHeight-kSafeAreaBottomHeight);
        _messageTBview.delegate = self;
        _messageTBview.dataSource = self;
        _messageTBview.emptyDataSetSource = self;
        _messageTBview.emptyDataSetDelegate = self;
        _messageTBview.tag = 10;
        _messageTBview.separatorStyle = 0;
        WeakSelf(weakSelf);
        _messageTBview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
                [weakSelf getDataWithURL];
            
        }];
    }
    return _messageTBview;
}

-(NSMutableArray *)listTBArray{
    if (!_listTBArray) {
        _listTBArray = [[NSMutableArray alloc]init];
    }
      return _listTBArray;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
